package net.etfbl.projekat.verko.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import net.etfbl.projekat.verko.dto.Zanr;

public class ZanrDAO {
	
	public static ArrayList<Zanr> getAllZanr(DataManager dm)
	{
		Connection conn=dm.checkOutConnection();
		ArrayList<Zanr> rezultat= new ArrayList<Zanr>();
		int id_zanr;
		String naziv;
		String opis;
		final String upit="select * from tbl_zanr";
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							id_zanr=rs.getInt(1);
							naziv=rs.getString(2);
							opis=rs.getString(3);
							rezultat.add(new Zanr(id_zanr,naziv,opis));
							}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u ZanrDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static void addZanr(DataManager dm, Zanr zanr)
	{
		final String upit="insert into tbl_zanr values(null, ?, ?)";
		
		Connection conn= dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ps.setString(1, zanr.getNaziv());
					ps.setString(1, zanr.getOpis());
					ps.execute();
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u ZanrDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
	}
	
	public static void updateZanr(DataManager dm, Zanr zanr)
	{
		final String upit="update tbl_zanr set naziv=?, opis=? where id_zanr = "+zanr.getId_zanr();
		
		Connection conn= dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ps.setString(1, zanr.getNaziv());
					ps.setString(2, zanr.getOpis());
					ps.execute();
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska ZanrDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
	}
	
	public static ArrayList<Zanr> filterByIme(DataManager dm, Zanr zanr)
	{
		final String upit="select * from tbl_zanr where naziv="+zanr.getNaziv();
		Connection conn = dm.checkOutConnection();
		ArrayList<Zanr> rezultat=new ArrayList<Zanr>();
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							rezultat.add(new Zanr(rs.getInt(1),rs.getString(2),rs.getString(3)));
						}
						}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u ZanrDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static ArrayList<Zanr> filterById(DataManager dm, Zanr zanr)
	{
		final String upit="select * from tbl_zanr where id_zanr="+zanr.getId_zanr();
		Connection conn = dm.checkOutConnection();
		ArrayList<Zanr> rezultat=new ArrayList<Zanr>();
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							rezultat.add(new Zanr(rs.getInt(1),rs.getString(2),rs.getString(3)));
						}
						}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u ZanrDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
}